Page({
  data: {
    nvabarData: {
      title: '我的学生',
      transmit: ''
    },
    searchKeyword: '',
    students: [],
    displayStudents: []
  },

  onLoad: function () {
    // 加载学生数据
    this.loadStudentData();
  },

  onShow: function () {
    // 页面显示时刷新数据
    this.loadStudentData();
  },

  // 加载学生数据
  loadStudentData: function() {
    // 模拟数据 - 实际开发中应从服务器获取
    const students = [
      {
        id: 1,
        name: '张小萌',
        avatar: '',
        phone: '138****5678',
        status: 'active'
      },
      {
        id: 2,
        name: '李小明',
        avatar: '',
        phone: '139****1234',
        status: 'active'
      },
      {
        id: 3,
        name: '王小红',
        avatar: '',
        phone: '136****8765',
        status: 'active'
      },
      {
        id: 4,
        name: '赵小刚',
        avatar: '',
        phone: '137****4321',
        status: 'active'
      },
      {
        id: 5,
        name: '刘小华',
        avatar: '',
        phone: '135****9876',
        status: 'active'
      },
      {
        id: 6,
        name: '陈小丽',
        avatar: '',
        phone: '134****3456',
        status: 'active'
      },
      {
        id: 7,
        name: '杨小强',
        avatar: '',
        phone: '133****6543',
        status: 'active'
      },
      {
        id: 8,
        name: '黄小燕',
        avatar: '',
        phone: '132****7890',
        status: 'active'
      },
      {
        id: 9,
        name: '周小伟',
        avatar: '',
        phone: '131****2345',
        status: 'active'
      },
      {
        id: 10,
        name: '吴小芳',
        avatar: '',
        phone: '130****6789',
        status: 'active'
      },
      {
        id: 11,
        name: '郑小杰',
        avatar: '',
        phone: '139****1122',
        status: 'active'
      },
      {
        id: 12,
        name: '王小虎',
        avatar: '',
        phone: '138****3344',
        status: 'active'
      },
      {
        id: 13,
        name: '冯小兰',
        avatar: '',
        phone: '137****5566',
        status: 'active'
      },
      {
        id: 14,
        name: '程小辉',
        avatar: '',
        phone: '136****7788',
        status: 'active'
      },
      {
        id: 15,
        name: '袁小梅',
        avatar: '',
        phone: '135****9900',
        status: 'active'
      },
      {
        id: 16,
        name: '邓小龙',
        avatar: '',
        phone: '134****2233',
        status: 'active'
      },
      {
        id: 17,
        name: '许小云',
        avatar: '',
        phone: '133****4455',
        status: 'active'
      },
      {
        id: 18,
        name: '谢小鹏',
        avatar: '',
        phone: '132****6677',
        status: 'active'
      },
      {
        id: 19,
        name: '唐小雨',
        avatar: '',
        phone: '131****8899',
        status: 'active'
      },
      {
        id: 20,
        name: '董小阳',
        avatar: '',
        phone: '130****0011',
        status: 'active'
      }
    ];

    this.setData({
      students,
      displayStudents: students
    });
  },

  // 搜索输入
  onSearchInput: function(e) {
    const keyword = e.detail.value;
    this.setData({
      searchKeyword: keyword
    });
    this.filterStudents(keyword);
  },

  // 搜索确认
  onSearchConfirm: function() {
    this.filterStudents(this.data.searchKeyword);
  },

  // 清除搜索
  clearSearch: function() {
    this.setData({
      searchKeyword: ''
    });
    this.filterStudents('');
  },

  // 过滤学生
  filterStudents: function(keyword) {
    if (!keyword.trim()) {
      this.setData({
        displayStudents: this.data.students
      });
      return;
    }

    const filteredStudents = this.data.students.filter(student => 
      student.name.includes(keyword) || 
      student.phone.includes(keyword.replace(/\*/g, ''))
    );

    this.setData({
      displayStudents: filteredStudents
    });
  },


  // 查看学生详情
  viewStudentDetail: function(e) {
    const student = e.currentTarget.dataset.student;
    wx.navigateTo({
      url: `/pages/student-detail/index?student=${JSON.stringify(student)}`
    });
  }
});